home *** CD-ROM | disk | FTP | other *** search
- #include "global.h"
-
-
- /* Given a working directory and an arbitrary pathname, resolve them into
- * an absolute pathname. Memory is allocated for the result, which
- * the caller must free
- */
- char *
- pathname(cd,path)
- char *cd; /* Current working directory */
- char *path; /* Pathname argument */
- {
- register char *buf,*cp;
- char *cdtmp,*pathtmp;
- char BaseVolume[255];
- int iovRef, e;
- char *PtoCstr(), *ptr;
-
- /*
- * Need to determine if the volume name is prepended
- * and if so remove it. It will be put back later.
- */
-
- if ( ( ptr = index(cd, ':')) != NULLCHAR)
- {
- sprintf(cd, "%s", ++ptr);
- }
-
- if(cd == NULLCHAR || path == NULLCHAR)
- return NULLCHAR;
-
- /* Strip any leading white space on args */
- while(*cd == ' ' || *cd == '\t')
- cd++;
- while(*path == ' ' || *path == '\t')
- path++;
-
- /* Allocate and initialize output buffer; user must free */
-
- buf = malloc((unsigned)strlen(cd) + strlen(path) + 10); /* fudge factor */
-
- /* Interpret path relative to cd only if it doesn't begin with ":" */
-
- if(path[0] != ':')
- crunch(buf, cd);
-
- crunch(buf,path);
-
- /* Special case: null final path means the root directory */
- if(buf[0] == '\0'){
- buf[0] = ':';
- buf[1] = '\0';
- }
- /*
- * Now find the Volume name and put it back.
- */
- setmem(BaseVolume,0, sizeof(BaseVolume));
-
- if ( ( e = GetVol(BaseVolume, &iovRef) ) != 0)
- {
- printf("pathname: could not get the volume name.\n");
- path[0] = '\0';
- return(path);
- }
- else
- {
- (void) PtoCstr(BaseVolume);
- }
-
- if ( ( ptr = malloc(strlen(BaseVolume)+strlen(buf))) == NULLCHAR)
- {
- printf("pathname: could not allocate memory.\n");
- return(NULLCHAR);
- }
- else
- {
- sprintf(ptr, "%s%s", BaseVolume, buf);
- free(buf);
- }
- return ptr;
- }
-
- /* Process a path name string, starting with and adding to
- * the existing buffer
- */
- static
- crunch(buf,path)
- char *buf;
- register char *path;
- {
- register char *cp;
-
-
- cp = buf + strlen(buf); /* Start write at end of current buffer */
-
- /* Now start crunching the pathname argument */
-
- while ( *path == ':' )
- {
- /* Hop up a level */
- if((cp = rindex(buf,':')) == NULLCHAR)
- {
- cp = buf; /* Don't back up beyond root */
- *cp++ = ':';
- }
- *cp = '\0'; /* In case there's another / */
- path++; /* Skip ":" */
- }
-
- if(*path == '\0')
- {
- *cp++ = '\0'; /* no more, all done */
- return;
- }
- /* Look for parent directory references, either at the end
- * of the path or imbedded in it
- */
- for(;;)
- {
- if(*path == '\0')
- {
- break; /* no more, all done */
- }
- if(strcmp(path,"::") == 0 )
- {
- /* Hop up a level */
- if((cp = rindex(buf,':')) == NULLCHAR)
- cp = buf; /* Don't back up beyond root */
- *cp = '\0'; /* In case there's another :: */
- path += 2; /* Skip "::" */
- while(*path == ':') /* Skip one or more slashes */
- {
- path++;
- }
- }
- else
- {
- if ( *path == ':' )
- path++;
- /* Ordinary name, copy up to next '/' or end of path */
- *cp++ = ':';
- while(*path != ':' && *path != '\0')
- *cp++ = *path++;
- }
- }
- *cp++ = '\0';
- }
-